home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / util / time / backclock.lha / BackClock / sources / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  8.1 KB  |  262 lines

  1. /*****************************************************************************
  2.  * 
  3.  * Nom                : utils.c
  4.  * desc                : utilitaires projet
  5.  *
  6.  * version            : $VER: utils.c 1.2 (08.02.98)
  7.  *
  8.  *****************************************************************************
  9.  */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <math.h>
  14. #include <exec/exec.h>
  15. #include <exec/ports.h>
  16. #include <exec/io.h>
  17. #include <exec/tasks.h>
  18. #include <exec/libraries.h>
  19. #include <dos/datetime.h>
  20. #include <dos/dosextens.h>
  21. #include <workbench/workbench.h>
  22. #include <libraries/notifyintuition.h>
  23.  
  24. #include <proto/exec.h>
  25. #include <proto/intuition.h>
  26. #include <proto/dos.h>
  27. #include <proto/graphics.h>
  28. #include <proto/gadtools.h>
  29. #include <proto/icon.h>
  30. #include "utils.h"
  31. #include "timer.h"
  32. #include "tracewin.h"
  33. #include "notify.h"
  34. #include "partial.h"
  35.  
  36. struct Library * NotifyIntuitionBase ;
  37.  
  38. idWin * init(WBArg * appName) {
  39.   struct DiskObject * dobj ;
  40.   char ** toolarray ;
  41.   struct Process * mytask = NULL ;
  42.   struct Screen  * WBScreen = NULL ;
  43.   idWin * prjWin = NULL ;
  44.   LONG  args[10] ;
  45.   
  46.   if ((prjWin = AllocVec(sizeof(idWin), MEMF_PUBLIC|MEMF_CLEAR)) != NULL) {
  47.     if ((prjWin->date = AllocVec(sizeof(struct DateStamp), MEMF_PUBLIC)) != NULL) {
  48.       if ((prjWin->Notify = AllocVec(sizeof(struct IntNotifyRequest), MEMF_PUBLIC|MEMF_CLEAR)) != NULL) {
  49.         if ((NotifyIntuitionBase = OpenLibrary(NOTIFYINTUITIONNAME, NOTIFYINTUITION_VMIN)) != NULL) {
  50.           mytask = (struct Process *)FindTask(NULL) ;
  51.         
  52.           if (mytask->pr_CLI){
  53.             //lance du cli
  54.             // lecture des args
  55.             if ((prjWin->args = ReadArgs("X=POSX/N/A,Y=POSY/N/A,W=WIDTH/N/A,H=HEIGHT/N/A,BACK/N/A", args, NULL)))
  56.               setArgs(prjWin, (LONG*)args) ;
  57.             else {
  58.               prjWin->backWin.back   = 3 ;
  59.               prjWin->backWin.posX   = 0 ;
  60.               prjWin->backWin.posY   = 0 ;
  61.               prjWin->backWin.width  = 100 ;
  62.               prjWin->backWin.height = 100 ;
  63.             }
  64.           } else {
  65.             //lance du wb
  66.             // lecture des tooltypes
  67.             CurrentDir(appName->wa_Lock) ;
  68.             if ((dobj=GetDiskObject(appName->wa_Name)) != NULL) {
  69.               toolarray = (char **)dobj->do_ToolTypes;
  70.               prjWin->backWin.posX   = readToolValue(toolarray, "POSX", 0) ;
  71.               prjWin->backWin.posY   = readToolValue(toolarray, "POSY", 0) ;
  72.               prjWin->backWin.back   = readToolValue(toolarray, "BACK", 3) ;
  73.               prjWin->backWin.width  = readToolValue(toolarray, "WIDTH", 50) ;
  74.               prjWin->backWin.height = readToolValue(toolarray, "HEIGHT", 50) ;
  75.               FreeDiskObject(dobj) ;
  76.             }
  77.           }
  78.           WBScreen = LockPubScreen("Workbench") ;
  79.           prjWin->win = OpenWindowTags(NULL, WA_Left,    prjWin->backWin.posX,
  80.                                                       WA_Top,        prjWin->backWin.posY,
  81.                                                       WA_Width,    prjWin->backWin.width,
  82.                                                       WA_Height,    prjWin->backWin.height,
  83.                                                       WA_IDCMP,    IDCMP_CHANGEWINDOW|IDCMP_REFRESHWINDOW,
  84.                                                       WA_Flags,    WFLG_BORDERLESS,
  85.                                                       WA_ScreenTitle, "BackClock", 
  86.                                                       WA_NewLookMenus, TRUE,
  87.                                                       WA_PubScreenName, "Workbench", TAG_DONE) ;
  88.           WindowToBack(prjWin->win) ;
  89.           UnlockPubScreen(NULL, WBScreen) ;
  90.           initwin(prjWin) ;
  91.           initTimer(prjWin) ;
  92.           startNotify(prjWin) ;
  93.                                                       
  94.           // initialisation ...  (fenetre...)
  95.         }else {
  96.           FreeVec(prjWin->Notify) ;
  97.           FreeVec(prjWin->date) ;
  98.           FreeVec(prjWin) ;
  99.           prjWin = NULL ;
  100.         }
  101.       }else {
  102.         // error memoire notify
  103.         FreeVec(prjWin->date) ;
  104.         FreeVec(prjWin) ;
  105.         prjWin = NULL ;
  106.       }
  107.     }else {
  108.       // plus de memoire pour datestamp
  109.       // liberation du projet
  110.       FreeVec(prjWin) ;
  111.       prjWin = NULL ;
  112.     }
  113.     
  114.   } // prjWin = 0 si erreur memoire 
  115.   return prjWin ;
  116. }
  117. void setArgs(idWin * prj, LONG * args) {
  118.   prj->backWin.posX   = *(ULONG*)args[0] ;
  119.   prj->backWin.posY   = *(ULONG*)args[1] ;
  120.   prj->backWin.width  = *(ULONG*)args[2] ;
  121.   prj->backWin.height = *(ULONG*)args[3] ;
  122.   prj->backWin.back   = *(ULONG*)args[4] ;
  123. }
  124. void close(idWin * prj) {
  125.   if (prj) {                              // struct prj existe (<>0) 
  126.     endNotify(prj) ;
  127.     if (prj->win) CloseWindow(prj->win) ; // ferme la fenetre 
  128.     if (prj->date) FreeVec(prj->date) ;   // libere datestamp 
  129.     if (prj->Notify) FreeVec(prj->Notify) ; // libere struct IntNotify
  130.     closeTimer(prj) ;
  131.     if (prj->args) FreeArgs(prj->args) ;  // libere les args
  132.     FreeVec(prj) ;                        // libere la memoire 
  133.     if (NotifyIntuitionBase) CloseLibrary(NotifyIntuitionBase) ;
  134.   }
  135. }
  136. void getDate(idWin * prj) {
  137.   // remplit la structure (projet) avec la date
  138.   
  139.   char * ptrTime = NULL ;
  140.   struct DateTime * dt = NULL ;
  141.   
  142.   
  143.   DateStamp(prj->date) ;
  144.  
  145.   ptrTime = AllocVec(LEN_DATSTRING, MEMF_PUBLIC|MEMF_CLEAR) ;
  146.   
  147.   if (ptrTime) {
  148.     if ((dt = AllocVec(sizeof(struct DateTime), MEMF_PUBLIC)) != NULL) {
  149.       CopyMem(prj->date, &(dt->dat_Stamp), sizeof(struct DateStamp)) ;
  150.       dt->dat_Format = FORMAT_DOS ;
  151.       dt->dat_Flags = NULL ;
  152.       dt->dat_StrDay  = NULL ;
  153.       dt->dat_StrDate = NULL ;
  154.       dt->dat_StrTime = ptrTime ;
  155.       DateToStr(dt) ;
  156.       strcpy(prj->datestr, ptrTime) ;
  157.       prj->oldH = prj->heu ;
  158.       prj->oldM = prj->min ;
  159.       prj->oldS = prj->sec ;
  160.       DateToByte(ptrTime, prj) ;
  161.       FreeVec(dt) ;
  162.     }
  163.   } 
  164.   if (ptrTime) FreeVec(ptrTime) ;
  165. }
  166.  
  167. void processwin(idWin* prj) {
  168.   /* variables
  169.    */
  170.   ULONG signals = NULL,                                         /* masque des signaux a attendre */
  171.         mask,                                                            /* masque des signaux recus */
  172.         sigtimer ;
  173.         
  174.   BOOL  stop    = FALSE ;                                                           /* vrai si ctrl c */
  175.   struct IntuiMessage * msg = NULL ;                     /* ptr sur message intuition */
  176.   struct MsgPort      * stopPort = NULL ;
  177.   struct Message      * stopmsg = NULL ;
  178.   
  179.   stopPort = CreateMsgPort() ;
  180.   stopPort->mp_Node.ln_Name = AllocVec(10, MEMF_PUBLIC) ;
  181.   strcpy(stopPort->mp_Node.ln_Name, "backclock") ;
  182.   AddPort(stopPort) ;
  183.   
  184.   sigtimer =  (1<<(prj->treq->tr_node.io_Message.mn_ReplyPort->mp_SigBit)) ; 
  185.   /* signaux d'attentes
  186.    */
  187.   signals = SIGBREAKF_CTRL_C | 
  188.             (1<<(prj->notifyPort->mp_SigBit)) | 
  189.             (1<<(prj->win->UserPort->mp_SigBit)) | sigtimer|
  190.             (1<<(stopPort->mp_SigBit)) ;
  191.   
  192.   runtimer(prj) ;
  193.   getDate(prj) ;
  194.   retracer(prj) ;
  195.   while(!stop) {
  196.     mask = Wait(signals) ;
  197.     if (mask & SIGBREAKF_CTRL_C) {
  198.       /* recu le signal CTRL C
  199.        */
  200.       stop=TRUE;
  201.     }
  202.     if (mask & (1<<(stopPort->mp_SigBit))) {
  203.       // recu stop
  204.       stopmsg = GetMsg(stopPort) ;
  205.       if (stopmsg) ReplyMsg(stopmsg) ;
  206.       stop=TRUE
  207.     }
  208.     if (mask & (1<<(prj->notifyPort->mp_SigBit))) {
  209.       // attention fermeture du WB
  210.       partialClose(prj) ;
  211.       partialOpen(prj) ;
  212.     }
  213.     if (mask & 1<<(prj->win->UserPort->mp_SigBit)) {
  214.       /* recu message d'intuition
  215.        */
  216.       while((msg = GT_GetIMsg(prj->win->UserPort)) != NULL) {
  217.         if ((msg->Class) == IDCMP_REFRESHWINDOW) {
  218.           /* rafraichissement (prob ?)
  219.            */
  220.           initwin(prj) ;
  221.           getDate(prj) ;
  222.           effacer(prj) ;
  223.           retracer(prj) ;
  224.         }
  225.         if ((msg->Class) == IDCMP_CHANGEWINDOW) 
  226.           WindowToBack(prj->win) ;
  227.         /* retourne le message
  228.          */
  229.         if (msg) ReplyMsg((struct Message *)msg) ; 
  230.       }
  231.     }
  232.     if (CheckIO((struct IORequest*)prj->treq)) {
  233.       WaitIO((struct IORequest*)prj->treq) ;
  234.       AbortIO((struct IORequest*)prj->treq) ;
  235.       WaitIO((struct IORequest*)prj->treq) ;
  236.       runtimer(prj) ;
  237.       getDate(prj) ;
  238.       effacer(prj) ;
  239.       retracer(prj) ;
  240.       WindowToBack(prj->win) ;
  241.     }  
  242.  
  243.   }
  244.   AbortIO((struct IORequest*)prj->treq) ;
  245.   WaitIO((struct IORequest*)prj->treq) ;
  246.   RemPort(stopPort) ;
  247.   FreeVec(stopPort->mp_Node.ln_Name) ;
  248.   DeleteMsgPort(stopPort) ;
  249.      
  250. }
  251.  
  252. int readToolValue(char ** toolarr, char * argtype, int argdef) {
  253.   int retVal = NULL ;
  254.   char * s ;
  255.  
  256.   if ((s = (char *) FindToolType((UBYTE **)toolarr, argtype)) != NULL) {
  257.     retVal = atoi(s) ;
  258.   } else retVal = argdef ;
  259.   return retVal ;
  260. }
  261.  
  262.